import Vue from 'vue'
import Vuex from 'vuex'
import { createVuexPersistedState } from 'vue-persistedstate'
import { getUserInfo } from '@/api/user'

Vue.use(Vuex)

export default new Vuex.Store({
  // 配置vuex插件
  plugins: [createVuexPersistedState()],

  state: {
    token: '',
    userInfo: {}
  },
  getters: {
  },
  mutations: {
    setToken (state, newToken) {
      state.token = newToken
    },
    setUserInfo (state, newUserInfo) {
      state.userInfo = newUserInfo
    }
  },
  actions: {
    async initUserInfo (ctx) {
      const { data: res } = await getUserInfo()
      if (res.code !== 0) return
      ctx.commit('setUserInfo', res.data)
    }
  },
  modules: {
  }
})
